/* 
    WhaleSpotter
    Copyright (C) 2007  Bob Mottram
    fuzzgun@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace whalespotter
{
    public class tailPoint
    {
        public const int densityHistogramLevels = 12;

        public int x, y, intensity;
        public int Type, Orientation, offset;
        public ArrayList connections = null;
        public int[,] densityHistogram = null;
        public bool matched = false;

        public tailPoint(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public tailPoint Copy()
        {
            tailPoint pt = new tailPoint(x, y);
            pt.Type = Type;
            pt.Orientation = Orientation;
            pt.matched = matched;

            if (densityHistogram != null)
            {
                pt.densityHistogram = new int[3, densityHistogramLevels];
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < densityHistogramLevels; j++)
                        pt.densityHistogram[i, j] = densityHistogram[i, j];
            }
            return (pt);
        }

        /// <summary>
        /// update the density histogram for this feature
        /// </summary>
        /// <param name="featureType"></param>
        /// <param name="distance"></param>
        public void updateDensityHistogram(int featureType, int distance)
        {
            int level = distance * densityHistogramLevels / 200;
            if (level >= densityHistogramLevels) level = densityHistogramLevels - 1;

            if (densityHistogram == null)
                densityHistogram = new int[3, densityHistogramLevels];

            densityHistogram[featureType, level]++;
        }

        public tailPoint(int x, int y, int Type, int Orientation, int intensity)
        {
            this.x = x;
            this.y = y;
            this.Type = Type;
            this.Orientation = Orientation;
            this.intensity = intensity;
        }

        /// <summary>
        /// save the tail point to file
        /// </summary>
        /// <param name="binfile"></param>
        public void Save(BinaryWriter binfile)
        {
            binfile.Write(x);
            binfile.Write(y);
            binfile.Write(intensity);
            binfile.Write(Type);
            binfile.Write(Orientation);
            if (densityHistogram == null)
                binfile.Write((int)0);
            else
            {
                binfile.Write(densityHistogramLevels);
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < densityHistogramLevels; j++)
                        binfile.Write(densityHistogram[i, j]);
            }
        }

        /// <summary>
        /// load a tail point from file
        /// </summary>
        /// <param name="binfile"></param>
        public void Load(BinaryReader binfile)
        {
            x = binfile.ReadInt32();
            y = binfile.ReadInt32();
            intensity = binfile.ReadInt32();
            Type = binfile.ReadInt32();
            Orientation = binfile.ReadInt32();
            int histogramLevels = binfile.ReadInt32();
            if (histogramLevels > 0)
            {
                densityHistogram = new int[3, densityHistogramLevels];
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < histogramLevels; j++)
                        densityHistogram[i, j] = binfile.ReadInt32();
            }
        }


    }
}
